import modelExtend from 'dva-model-extend'
import { query, info, remove, update, save, jurisdiction1 } from 'services/role'
import { query as menuSelect } from 'services/menu'

import { pageModel } from 'models/common'

export default modelExtend(pageModel, {

  namespace: 'role',
  state: {
    treeData: [],
    treeCheckedKeys: [],
    currentItem: {},
    modalVisible: false,
    modalType: 'create',
    selectedRowKeys: [],
    jurisdictionList:[],
    showDetails:false,
  },
  subscriptions: {
    setup ({ dispatch, history }) {
      history.listen((location) => {
        if (location.pathname === '/sys/role') {
          dispatch({ type: 'query',
            payload: {
              ...location.query,
            } })
          dispatch({ type: 'jurisdiction11',
            payload: {
              ...location.query,
            } })
        }else{
          dispatch({ type: 'clear'})
        }
      })
    },
  },

  effects: {
  	*jurisdiction11({payload},{call,put}){
  		const data = yield call(jurisdiction1,payload)
  		if(data){
  			yield put({
  				type:'jurisdictionSuccess',
  				payload:{
  					jurisdictionList:data.organizationList
  				}
  			})
  		}
  	},
    * query ({
      payload,
    }, { call, put }) {
      const data = yield call(query, payload)
      if (data.success) {
        yield put({
          type: 'querySuccess',
          payload: {
            list: data.page.list,
            pagination: {
              current: Number(data.page.current) || 1,
              pageSize: Number(data.page.pageSize) || 10,
              total: data.page.totalCount,
            },
          },
        })
      } else {
        throw data
      }
    },
    * delete ({ payload }, { call, put, select }) {
      const roleIds = [payload.id]

      const data = yield call(remove, { roleIds })
      const { selectedRowKeys } = yield select(_ => _.role)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: selectedRowKeys.filter(_ => _ !== payload) } })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },
    * multiDelete ({ payload }, { call, put }) {
      const data = yield call(remove, payload)
      if (data.success) {
        yield put({ type: 'updateState', payload: { selectedRowKeys: [] } })
        yield put({ type: 'query' })
      } else {
        throw data
      }
    },
    * add ({ payload }, { call, put }) {
      const data = yield call(menuSelect, payload)
      if (data.success) {
        yield put({
          type: 'showModal',
          payload: {
            treeData: data.list,
            treeCheckedKeys: [],
            modalType: 'create',
          },
        }),
        yield put({type:'showDetails'})
      } else {
        throw data
      }
    },
    * edit ({ payload }, { call, put }) {
      const currentItem = yield call(info, payload)
      const data = yield call(menuSelect, payload)
      const role = currentItem.role
      const treeCheckedKeys = []
      if (role.menuResourcesList != undefined && role.menuResourcesList != null) {
        role.menuResourcesList.map((v,i) => {
          v.menuIdList.map((x,j)=>{
          	v.menuIdList[j] = x.toString()
          })
          treeCheckedKeys.push(v)
        })
      }
      if (data.success) {
        yield put({
          type: 'showModal',
          payload: {
            currentItem: role,
            treeCheckedKeys,
            treeData: data.list,
            modalType: 'update',
          },
        })
        yield put({type:"showDetails"})
      } else {
        throw data
      }
    },
    * create ({ payload }, { call, put }) {
      const data = yield call(save, payload)
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
        yield put({	type: 'hideDetails' })
      } else {
        throw data
      }
    },

    * update ({ payload }, { select, call, put }) {
      const roleId = yield select(({ role }) => role.currentItem.roleId)
      const newRole = { ...payload, roleId }
      const data = yield call(update, newRole)
      if (data.success) {
        yield put({ type: 'hideModal' })
        yield put({ type: 'query' })
        yield put({	type: 'hideDetails' })
      } else {
        throw data
      }
    },
    * showDetails({payload},{call,put}){
    		yield put({
    			type:'showDetailSuccess',
    			payload:{
    				showDetails:true
    			}
    		})
    },
    * hideDetails({payload},{call,put}){
    		yield put({
    			type:'hideDetailSuccess',
    			payload:{
    				showDetails:false,
    			}
    		})
    }
  },
  reducers:{
  	jurisdictionSuccess ( state, action ){
  		const { jurisdictionList } = action.payload
  		return{...state,
  			jurisdictionList
  		}
  	},
  	showDetailSuccess ( state, action ){
  		const { showDetails } = action.payload
  		return{...state,
  			showDetails
  		}
  	},
  	hideDetailSuccess ( state, action ){
  		const { showDetails } = action.payload
  		return{...state,
  			showDetails
  		}
  	},
  },
})
